/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models.Deck;
import myTest.MYTest;
import java.util.ArrayList;
import models.Card.Card;
import java.util.List;

/**
 *牌库
 * @author Invince
 */
public class Deck {
/*----------Attributs-------------------*/
    /**
     * 牌库的id
     */
    protected int id;
    /**
     * 牌
     */
    protected List<Card> listCard;
  
/*----------Public functions------------*/
    /**
     * 加一张牌到牌库，只在新建时用，或者被其他函数调用，游戏中不用，游戏中的牌永远是一个牌库移到另一个牌库
     * @param card
     * @return
     */
    public boolean addCard(Card card){
        String r="Deck--addCard: "+card.getName();
        MYTest.println(r);
        return listCard.add(card);//Always true here
    }
     /**
     * 从牌库中删一张牌，只在新建时用，游戏中不用，游戏中的牌永远是一个牌库移到另一个牌库
     * @param card
     * @return
     */
    public boolean removeCard(int indexCard){
        String s="Deck--removeCard: "+listCard.get(indexCard).getName();
        String r="";
        boolean rb=false;
        if(indexCard<listCard.size()){
            listCard.remove(indexCard);
            rb=true;
        }
       if(!rb)
            r="Failed: ";
        r+=s;
        MYTest.println(r);
        return rb;
    }

    /**
     * 判断一张牌是否在牌库里
     * @param card
     * @return
     */
    public boolean containsCard(Card card){
        String s="Deck--containCard: "+card.getName();
        String r="";
        boolean rb=listCard.contains(card);
        if(!rb)
            r="Failed: ";
        r+=s;
        MYTest.println(r);
        return rb;
    }

    /**
     * 从souceDeck取第indexCard张牌移到本牌库
     * @param indexCard
     * @param sourceDeck
     * @return
     */
    public boolean drawCard(int indexCard,Deck sourceDeck){
        String s="Deck--drawCard: "+listCard.get(indexCard).getName()+" to anotherDeck";
        String r="";
        boolean rb=false;
        if(indexCard<sourceDeck.getListCard().size()){
            this.listCard.add(sourceDeck.getListCard().get(indexCard));
            sourceDeck.removeCard(indexCard);
            rb= true;
        }
        if(!rb)
            r="Failed: ";
        r+=s;
        MYTest.println(r);
        return false;
    }

     /**
     * 将本库第indexCard张牌移到targetDeck里去
     * @param indexCard
     * @param targetDeck    
     * @return
     */
    public boolean dropCard(int indexCard,Deck targetDeck){
        String s="Deck--dropCard："+listCard.get(indexCard).getName()+" to anotherDeck";
        String r="";
        boolean rb=targetDeck.drawCard(indexCard,this);
        if(!rb)
            r="Failed: ";
        r+=s;
        MYTest.println(r);
        return rb;
    }

    /**
     * 从sourceDeck取开头nbCard张牌移入本牌库
     * @param nbCard Attention here is nbCard
     * @param sourceDeck
     * @return
     */
    public boolean drawCardFromBaseDeck(int nbCard,Deck sourceDeck){
        boolean rb=true;
        MYTest.println("--Deck--drawListCard：begins--");
        if(nbCard>sourceDeck.getListCard().size())
            nbCard=sourceDeck.getListCard().size();
        for(int i=0;i<nbCard;i++){
            rb=rb && drawCard(0/*每次都是抽取第一张*/,sourceDeck);
        }
        MYTest.println("--Deck--drawListCard：ends--");
        return rb;
    }

    /**
     * 将牌顶nbCard张牌置于牌底
     * @param nbCard
     * @return
     */
     public boolean replaceBottom(int nbCard){
        MYTest.println("--Deck--replaceBottom: begins--");
        boolean rb= drawCardFromBaseDeck(nbCard,this);
        MYTest.println("--Deck--replaceBottom: ends--");
        return rb;
    }

    /**
     * 自动洗牌
     * @return
     */
    public boolean shuffle(){
        java.util.Collections.shuffle(this.listCard);
        MYTest.println("Deck--shuffle:automatically");
        return true;
    }

    /**
     * 手动洗单张牌，将第oldIndex张牌置于newIndex
     * @param oldIndex
     * @param newIndex
     * @return
     */
    public boolean shuffle(int oldIndex,int newIndex){
        String s="Deck--shuffle: manually";
        String r="";
        boolean rb=false;
        if(oldIndex<this.listCard.size() && newIndex<this.listCard.size()){
            Card card=this.listCard.get(oldIndex);
            this.listCard.remove(oldIndex);
            this.listCard.add(newIndex, card);
            rb=true;
        }
        if(!rb)
            r="Failed: ";
        r+=s;
        MYTest.println(r);
        return rb;
    }

    /**手动洗一组牌
     * 一行存一组old to new ,[i][0]存oldIndex，[i][1]存newIndex
     * @param correspondances
     * @return
     */
    public boolean shuffle(int correspondances[][]){
        MYTest.println("--Deck--shuffle list: begins--");
        boolean rb=true;
        int size=correspondances.length;//表示行数，总共有多少操作
        for(int i=0;i<size;i++){
           rb=rb && shuffle(correspondances[i][0],correspondances[i][1]);
        }
        MYTest.println("--Deck--shuffle list: ends--");
        return rb;
    }

    /**
     * 返回牌的数目
     * @return
     */
    public int size(){
        return this.listCard.size();
    }

    /**
     * 返回第index张牌
     * @param index
     * @return
     */
    public Card get(int index){
        return this.listCard.get(index);
    }

/*----------Private functions-----------*/
/*----------Constructor-----------------*/
    /**
     * Constructor void
     */
    public Deck(int id){
        this.id=id;
        this.listCard = new ArrayList<Card>();
    }
    /**
     * Constructor
     * @param listCard
     */
    public Deck(int id,List<Card> listCard) {
        this.id=id;
        this.listCard = listCard;
    }

/*----------Gets and sets---------------*/
    public List<Card> getListCard() {
        return listCard;
    }

    public void setListCard(List<Card> listCard) {
        this.listCard = listCard;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
}
